//
//  GJFPlotDetailViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2016/12/23.
//  Copyright © 2016年 郭静芬. All rights reserved.
//

#import "GJFPlotDetailViewController.h"
#import "GJFPlotDetailIntroductionTableViewCell.h"
#import "GJFPlotDetailImageTableViewCell.h"
#import "GJFPlotModel.h"
#import "GJFPlotDetailHeaderView.h"
#import "GJFToolView.h"
#import "GJFSuccessViewController.h"
#define HEADERHEIGHT 250 * YY

@interface GJFPlotDetailViewController ()<UITableViewDelegate, UITableViewDataSource, GJFToolViewDelegate, UMSocialShareMenuViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) GJFPlotDetailHeaderView *headerView;
@property (nonatomic, strong) GJFToolView *toolView;
@property (nonatomic, strong) GJFPlotModel *plotModel;
@end

@implementation GJFPlotDetailViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //设置导航栏的透明度
    for (UIView *v in self.navigationController.navigationBar.subviews) {
        v.alpha = 1.0;
    }
    [self.navigationController.navigationBar setBackgroundImage:[GJFSingleTool imageWithColor:HOMECOLOR] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 重置导航条背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsCompact];
    // 阴影效果的图片
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-返回"] style:UIBarButtonItemStyleDone target:self action:@selector(popVc)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-分享"] style:UIBarButtonItemStyleDone target:self action:@selector(shareAction)];
    self.tableView.tableHeaderView = self.headerView;
    [self scrollViewDidScroll:self.tableView];
    [self getPlotDetailData];
}
#pragma mark - 点击返回按钮pop到列表页面
- (void)popVc {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 点击分享
- (void)shareAction {
    [GJFSingleTool shareToViewController:self title:self.plotModel.title content:self.plotModel.share_desc image:self.plotModel.share_icon_res url:self.plotModel.share_url];
}
#pragma mark - 婚礼策划详情页的网络请求
- (void)getPlotDetailData {
    [self.view showBusyHUD];
    NSDictionary *dict = @{@"itemId":self.plotId?:@""};
    [GJFNetworkTool POSTRequestWithUrl:APP_PLOT_DETAIL params:dict complete:^(NSDictionary *result) {
        [self.view hideBusyHUD];
        if ([[result objectForKey:@"code"] intValue] == 1000) {
            self.plotModel = [GJFPlotModel mj_objectWithKeyValues:[result objectForKey:@"data"]];
            self.headerView.plotModel = self.plotModel;
            self.toolView.plotModel = self.plotModel;
            [self.tableView reloadData];
        } else {
            [self.view showWarning:result[@"msg"]];
        }
    }];
}
#pragma mark - TableView的分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
#pragma mark - TableView每个分区的row数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.plotModel.list.count;
    }
}
#pragma mark - TableView每个分区中row的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        GJFPlotDetailIntroductionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IntroductionCell"];
        cell.plotModel = self.plotModel;
        return cell;
    } else {
        GJFPlotDetailImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ImageCell"];
        cell.imageUrl = [self.plotModel.list[indexPath.row] img];
        return cell;
    }
}
#pragma mark - TableView每个分区中row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (!self.plotModel) {
            return 0.1;
        }
        else {
            return [self.plotModel.heightForCell intValue]?:370;
        }
    }
    return 220 * YY;
}

#pragma mark - GJFToolViewDelegate
- (void)clickZanBtnAction {
    NSString *url = [self.plotModel.is_zan boolValue]?APP_PLOT_UNZAN:APP_PLOT_ZAN;
    NSDictionary *dict = @{@"zanId": self.plotId?:@"", @"type":@1};
    [GJFNetworkTool POSTRequestWithUrl:url params:dict complete:^(NSDictionary *result) {
        if ([[result objectForKey:@"code"] intValue] == 1000) {
            self.plotModel.is_zan = [NSString stringWithFormat:@"%d", ![self.plotModel.is_zan boolValue]];
            self.toolView.plotModel = self.plotModel;
        }
        [self.view showWarning:[result objectForKey:@"msg"]];
    }];
}

- (void)clickCollectBtnAction {
    NSString *url = [self.plotModel.is_collect boolValue]?APP_PLOT_UNCOLLECT:APP_PLOT_COLLECT;
    NSDictionary *dict = @{@"collectId": self.plotId?:@"", @"type":self.plotModel.type};
    [GJFNetworkTool POSTRequestWithUrl:url params:dict complete:^(NSDictionary *result) {
        if ([[result objectForKey:@"code"] intValue] == 1000) {
            self.plotModel.is_collect = [NSString stringWithFormat:@"%d", ![self.plotModel.is_collect boolValue]];
            self.toolView.plotModel = self.plotModel;
        }
        [self.view showWarning:[result objectForKey:@"msg"]];
    }];
}

- (void)clickOrderBtnAction {
    GJFSuccessViewController *success = [[GJFSuccessViewController alloc] initWithNibName:NSStringFromClass([GJFSuccessViewController class]) bundle:nil];
    success.merchantTitleStr = self.plotModel.title;
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:success];
    success.navigationItem.title = @"预约申请成功";
    [self presentViewController:navi animated:YES completion:^{}];
}
- (void)clickTelBtnAction {
    NSMutableString * str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@", self.plotModel.telephone];
    NSURL *url = [NSURL URLWithString:str];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
    else {
        [self.view showWarning:@"不支持电话功能"];
    }
}
#pragma mark - 当scrollView将要拉动时，导航栏将会出现
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.navigationItem.title = self.plotModel.title;
}
#pragma mark - 根据scrollView偏移量的改变，最上面封装的视图发生变化，以及导航栏透明度的改变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //获取偏移量
    CGPoint offset = scrollView.contentOffset;
    // 更改导航栏透明度
    CGFloat alpha = offset.y / (HEADERHEIGHT - 50);
    if (alpha >= 1) {
        alpha = 0.99;
    }
    NSLog(@"%f", alpha);
    // 设置导航条的背景图片 其透明度随  alpha 值 而改变
    for (UIView *v in self.navigationController.navigationBar.subviews) {
        NSString *vStr = NSStringFromClass(v.class);
        if ([vStr isEqualToString:@"UINavigationButton"]) {
            [v setAlpha:1.0];
        }
        else {
            [v setAlpha:alpha];
        }
    }
    UIImage *image = [GJFSingleTool imageWithColor:[UIColor colorWithRed:0.46 green:0.17 blue:0.28 alpha:alpha]];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

#pragma mark - 懒加载
- (GJFPlotDetailHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[GJFPlotDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEADERHEIGHT)];
    }
    return _headerView;
}
- (GJFToolView *)toolView {
    if (!_toolView) {
        _toolView = [[GJFToolView alloc] initWithFrame:CGRectMake(0, HEIGHT - 49 - 64, WIDTH, 49)];
        _toolView.delegate = self;
        [self.view addSubview:_toolView];
    }
    return _toolView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, WIDTH, HEIGHT - 49) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        _tableView.bounces = NO;
        [_tableView registerClass:[GJFPlotDetailIntroductionTableViewCell class] forCellReuseIdentifier:@"IntroductionCell"];
        [_tableView registerClass:[GJFPlotDetailImageTableViewCell class] forCellReuseIdentifier:@"ImageCell"];

    }
    return _tableView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
